<?php

namespace app\dao\office;

use app\dao\BaseDao;
use app\model\exam\ExamRecord;
use app\model\exam\ExamSend;
use app\model\office\OfficeClass;
use app\model\office\OfficeUser;
use think\exception\ErrorException;


class  OfficeStudentDao extends BaseDao
{

    public function index(array $where)
    {
        return $this->getModel()::with(['majors' => function ($query) {
            $query->withoutField('website_id,level,major_code,create_time,update_time,delete_time');
        }])->withoutField('update_time,delete_time,website_id')
            ->where('website_id', $where['website_id'])
            ->order('id', 'desc')
            ->paginate($where['pageSize']);
    }

    public function create(array $where)
    {
        return $this->getModel()->create($where);
    }

    public function updateDao(array $where)
    {
        return $this->getModel()->update($where);
    }

    public function deleteDao(array $where)
    {
        return $this->getModel()->whereIn('id', $where['id'])->update(['delete_time' => date('Y-m-d H:i:s', time())]);
    }

    public function searchDao(array $where)
    {
        return $this->getModel()->with(['majors' => function ($query) {
            $query->withoutField('website_id,level,major_code,create_time,update_time,delete_time');
        }])->withSearch([
            'major_id', 'name', 'sex', 'idcard', 'phone', 'exam_number', 'enter_batch', 'enter_type', 'student_number'
        ], ['major_id' => $where['major_id'], 'name' => $where['name'],
            'sex' => $where['sex'], 'idcard' => $where['idcard'],
            'phone' => $where['phone'], 'exam_number' => $where['exam_number'],
            'enter_batch' => $where['enter_batch'], 'enter_type' => $where['enter_type'],
            'student_number' => $where['student_number']])
            ->where('website_id', $where['website_id'])
            ->order('id', 'desc')
            ->paginate($where['pageSize']);
    }

    public function getUserList(array $where)
    {
        return $this->getModel()->withSearch(['name'], ['name' => $where['name']])->field('id,name,idcard')->where('website_id', $where['website_id'])->paginate($where['pageSize']);
    }

    public function getIdsFormUser(array $where)
    {
        return $this->getModel()->with(['majors' => function ($query) {
            $query->withoutField('website_id,level,major_code,create_time,update_time,delete_time');
        }])->withSearch([
            'major_id', 'name', 'sex', 'idcard', 'phone', 'exam_number', 'enter_batch', 'enter_type', 'student_number'
        ], ['major_id' => $where['major_id'], 'name' => $where['name'],
            'sex' => $where['sex'], 'idcard' => $where['idcard'],
            'phone' => $where['phone'], 'exam_number' => $where['exam_number'],
            'enter_batch' => $where['enter_batch'], 'enter_type' => $where['enter_type'],
            'student_number' => $where['student_number']])
            ->whereIn('id', $where['ids'])
            ->order('id', 'desc')
            ->paginate($where['pageSize']);
    }

    public function info(array $where)
    {
        return $this->search()->with(['majors' => function ($query) {
            $query->field('id,title,level,major_code');
        }])->withoutField('create_time,update_time,delete_time,pwd')->find($where['userId']);
    }

    public function excelOutFromIds(array $where)
    {
        return $this->getModel()::with(['majors' => function ($query) {
            $query->withoutField('website_id,level,major_code,create_time,update_time,delete_time');
        }])->withoutField('update_time,delete_time,website_id')
            ->order('id', 'desc')
            ->select(explode(',',$where['ids']));
    }

    public function selectAllSearchToExcel(array $where)
    {
        return $this->getModel()->with(['majors' => function ($query) {
            $query->withoutField('website_id,level,major_code,create_time,update_time,delete_time');
        }])->withSearch([
            'major_id', 'name', 'sex', 'idcard', 'phone', 'exam_number', 'enter_batch', 'enter_type', 'student_number'
        ], ['major_id' => $where['major_id'], 'name' => $where['name'],
            'sex' => $where['sex'], 'idcard' => $where['idcard'],
            'phone' => $where['phone'], 'exam_number' => $where['exam_number'],
            'enter_batch' => $where['enter_batch'], 'enter_type' => $where['enter_type'],
            'student_number' => $where['student_number']])
            ->where('website_id', $where['website_id'])
            ->withoutField('website_id')
            ->order('id', 'desc')
            ->select();
    }

    protected function setModel(): string
    {
        return OfficeUser::class;
    }


//     ========================================================= api ====================================================

    /*
     * 获取学生基本资料
     */
    public function getUserInformation(array $where)
    {
        return $this->getModel()
            ->with(['majors' => function ($query) {
                $query->withField(['id', 'level','title']);
            }])
            ->withoutField(['create_time', 'update_time', 'delete_time', 'enter_type'])
            ->where('idcard', $where['idcard'])
            ->where('website_id', $where['website_id'])
            ->find();


    }

    /*
     * 修改密码
     */
    public function updataPwd(array $where)
    {

        return $this->getModel()
            ->where('idcard', $where['idcard'])
            ->where('website_id', $where['website_id'])
            ->update(['pwd' => $where['pwd']]);
    }

    /*
     * 用户登录
     */
    public function register(array $where)
    {
        return $this->getModel()
            ->where('idcard', $where['idcard'])
            ->where('website_id', $where['website_id'])
            ->find();

    }
    /*
     * 通过学生信息获取课程id
     */
    public function getClassId(array $where){
        $user = $this->getModel()
            ->where('idcard', $where['idcard'])
            ->field(['major_id', 'enter_batch'])
            ->find();
        $class = OfficeClass::where('major_id', $user['major_id'])
//            ->where('garde', $user['enter_batch'])
            ->field('id')
            ->select();
        $list = [];
        for ($m = 0; $m < count($class); $m++) {
            $list[] = $class[$m]['id'];
        }
        return $list;
    }

    /*
     * 通过学生获取考试列表
     */
    public function getTest(array $where)
    {

            $task = ExamSend::
                whereIn('class_id', $where['list'])
//                ->with(['OfficeClass' => function ($query) {
//                    $query->withField(['id', 'title']);
//                }, 'paper' => function ($query) {
//                    $query->withField(['id', 'title']);
//                }])
                ->where('status', 1)
                ->where('pattern', $where['pattern'])
                ->order('id', 'asc')
                ->withoutField(['create_time', 'update_time', 'delete_time', 'status'])
                ->select();
            return $task;


    }

    /*
     * 获取学生考试信息
     */
    public function getExam(array $where){
        return ExamRecord::
            whereIn('class_id',$where['classid'])
            ->where('idcard',$where['idcard'])
            ->where('exam_type',$where['pattern'])
            ->where('exam_id',$where['exam_id'])
            ->field(['id','exam_id','total_score',])
            ->select();


    }

    /*
     * 获取考试成绩
     */
    public function getExamRecord(array $where){
        $data = ExamRecord::
            with(['exam'=>function($requery){
            $requery->field(['id','title','pass_score']);
        }])
            ->where('idcard',$where['idcard'])
            ->whereIn('class_id',$where['list'])
            ->where('exam_type',$where['pattern'])
            ->where('read_status',1)
            ->field(['id','exam_id','total_score',])
            ->select();
        return $data;
    }
}